<?php
/*
 * 接口：/flow_approval/update
 * 说明：更新审核流程
 */

//关闭事物自动提交
$db->mysqlDB->falseAutoCommit();

//获取请求参数
$params = $route->bodyParams;


//更新审核流
$P1 = array(
  "update_where" => 'ofa_id',
  "ofa_id" => $params['ofa_id'],
  "ofa_root_osid" => $params['ofa_root_osid'],
  "ofa_son_osid" => $params['ofa_son_osid']
);
$R1 = $db->mysqlDB->automaticUpdate($P1)->update("easyd_oa_flow_approval");

//移除原有节点中的用户
$P2 = array(
  "update_where" => 'ofam_ofid,ofam_ofaid',
  "ofam_ofid" => $params['of_id'],
  "ofam_ofaid" => $params['ofa_id'],
  "ofam_is_delete" => 1
);

$R2 = $db->mysqlDB->automaticUpdate($P2)->update("easyd_oa_flow_approval_middle");

//添加审核流程节点与用户

//移除根节点数据信息
array_shift($params['flowData']);


//新增是否有出错的记录
$isAddError = false;

for($i=0;$i<count($params['flowData']);$i++){

  for($k=0;$k<count($params['flowData'][$i]['node_user']);$k++){

    $P3 = array(
      "ofam_osid"  => $params['flowData'][$i]['node_id'],
      "ofam_uid"   => $params['flowData'][$i]['node_user'][$k],
      "ofam_ofid"  => $params['of_id'],
      "ofam_ofaid" => $params['ofa_id']
    );

    $R3 = $db->mysqlDB->automaticAdd($P3)->insert("easyd_oa_flow_approval_middle");

    //如果有出错则进行记录
    if(!$R3){
      $isAddError = true;
      break;
    }

  }

}

//判断是否有出错
if(!$R1 || !$R2 || $isAddError){
  $db->mysqlDB->rollBackCommint();
  $db->mysqlDB->trueAutoCommit();
  $response->responseData(false);
}else{
  $db->mysqlDB->actionCommint();
  $db->mysqlDB->trueAutoCommit();
  $response->responseData(true);
}
?>